/**
 * $Id: DayOfWeek.java 59 2012-12-06 13:05:02Z Denis.Skalsky@gmail.com $
 */
package com.globallogic.skalskyy.javabasic.dayofweek;

import java.util.Calendar;
import java.util.Date;

import com.globallogic.skalskyy.javabasic.inputdata.ConsoleReader;
import com.globallogic.skalskyy.javabasic.inputdata.DataValidator;

/**
 * DayOfWeek finds day of the week using inputted date
 * @author $Author: Denis.Skalsky@gmail.com $
 * @version $Revision: 47 $
 */
public class DayOfWeek {
	
	private DataValidator dataValidator = new DataValidator();
	
	enum Days {
		TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY, MONDAY
	}	
	
	public Date getInputtedDate() {
		return this.dataValidator.getDateValue();
	}
	
	public void useConsoleReader() {
		this.dataValidator.setIncomingData(
				new ConsoleReader(ConsoleReader.ConsoleCommandPrompt.DAYOFWEEK));
	}
			
	public static String getDayOfWeak(Date date) {
		String result;
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		
		int week = 7;
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int month = calendar.get(Calendar.MONTH);
		int year = calendar.get(Calendar.YEAR);
		int[] dayShift = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
		
		if (month < 3) {
			year--;
		};
		
		int leapYears = year/4 + year/400 - year/100; 
		
		
		int indexDay = (year + leapYears + dayShift[month - 1] + day) % week; 
		result = Days.values()[indexDay].name();
		
		return result;
	}
}
